package as2.controller;

// Import required java libraries
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONException;
import org.json.JSONObject;

// Extend HttpServlet class
public class CheckoutShipping extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	// Method to handle GET method request.
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// Set response content type
		response.setContentType("application/json");
		StringBuffer jb = new StringBuffer();
		String line = null;
		try {
			BufferedReader reader = request.getReader();
			while ((line = reader.readLine()) != null)
				jb.append(line);
		} catch (Exception e) { /* report an error */
		}
		JSONObject jsonRequest = null;
		String city = null;
		try {
			jsonRequest = new JSONObject(jb.toString());
			city = jsonRequest.getString("city");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		String order = (String) request.getSession().getAttribute("cart");

		CloseableHttpClient client = HttpClients.createDefault();
		URIBuilder builder = null;
		HttpGet httpGet = null;
		try {
			builder = new URIBuilder("http://localhost:8081/Shipping/shipping");
			builder.setParameter("city", city).setParameter("order", order);

			httpGet = new HttpGet(builder.build());
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		CloseableHttpResponse response2 = client.execute(httpGet);

		BufferedReader rd = new BufferedReader(new InputStreamReader(response2
				.getEntity().getContent()));
		String responseStr = rd.readLine();

		JSONObject res = new JSONObject();
		try {
			Double cost = new Double(responseStr);
			res.append("shippingcost", cost);
			response.getWriter().print(res.toString());

			request.getSession().setAttribute("shippingcost", cost);
			request.getSession().setAttribute("addressCity", city);
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// Method to handle POST method request.
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
}